package cesdentalrecord.dao;

import java.io.IOException;
import java.util.ArrayList;
import java.util.List;

import org.apache.torque.util.Criteria;

import cesdentalrecord.dao.Student;
import cesdentalrecord.dao.controller.ViewAllStudentsController;

public class StudentManager extends cesdentalrecord.dao.BaseStudentPeer{

	
	public List<Student> searchAStudent(String surname, String firstName, String mi){
		Criteria criteria = new Criteria();
		Criteria.Criterion c1 = criteria.getNewCriterion(BaseStudentPeer.SURNAME, surname, Criteria.EQUAL);
		Criteria.Criterion c2 = criteria.getNewCriterion(BaseStudentPeer.FIRSTNAME, firstName, Criteria.EQUAL);
		Criteria.Criterion c3 = criteria.getNewCriterion(BaseStudentPeer.MI, mi, Criteria.EQUAL);
		criteria.add(c1.or(c2).or(c3));
		
		try{
			return (List<Student>)BaseStudentPeer.doSelect(criteria);
			
		}catch(Exception e){
			e.printStackTrace();
			return null;
		}
	}
	
	public Student viewAStudent(String surname, String firstName, String mi){
		Criteria criteria = new Criteria();
		Criteria.Criterion c1 = criteria.getNewCriterion(BaseStudentPeer.SURNAME, surname, Criteria.EQUAL);
		Criteria.Criterion c2 = criteria.getNewCriterion(BaseStudentPeer.FIRSTNAME, firstName, Criteria.EQUAL);
		Criteria.Criterion c3 = criteria.getNewCriterion(BaseStudentPeer.MI, mi, Criteria.EQUAL);
		criteria.add(c1.or(c2).or(c3));
		
		try{
			return (Student)BaseStudentPeer.doSelect(criteria).get(0);
			
		}catch(Exception e){
			e.printStackTrace();
			return null;
		}
	}

	public static boolean saveEditedStudentData(Student him,int me, String surname, String firstname,String mi,String gender,String grade, String section,String month,String day,String year,String bplace,String address,String parent,String occ,String medhist, int age ) throws IOException{
		Criteria criteria = new Criteria();

		criteria.add(BaseStudentPeer.STUDENTID, me);
		if(surname.length() == 0)
		criteria.add(BaseStudentPeer.SURNAME, him.getSurname());	
		else
		criteria.add(BaseStudentPeer.SURNAME, surname);		
		
		if(firstname.length() == 0)
		criteria.add(BaseStudentPeer.FIRSTNAME, him.getFirstname());
		
		else
		criteria.add(BaseStudentPeer.FIRSTNAME, firstname);
		
		if(mi.length() == 0)
		criteria.add(BaseStudentPeer.MI, him.getMi());
		else
		criteria.add(BaseStudentPeer.MI, mi);
		
		if(gender.length() == 0)
			criteria.add(BaseStudentPeer.GENDER, him.getGender());
			else
			criteria.add(BaseStudentPeer.GENDER, gender);
			
		
		if(grade.length() == 0)
		criteria.add(BaseStudentPeer.GRADE, him.getGrade());
		else
		criteria.add(BaseStudentPeer.GRADE, grade);
		
		if(section.length() == 0)
		criteria.add(BaseStudentPeer.SECTION, him.getSection());
		else
		criteria.add(BaseStudentPeer.SECTION, section);	
		
		if(month.length() == 0)
		criteria.add(BaseStudentPeer.BIRTHMONTH, him.getBirthmonth());
		else
		criteria.add(BaseStudentPeer.BIRTHMONTH, month);
		
		
		if(day.length() == 0)
		criteria.add(BaseStudentPeer.BIRTHDATE, him.getBirthdate());
		else
		criteria.add(BaseStudentPeer.BIRTHDATE, day);


		if(year.length() == 0)	
		criteria.add(BaseStudentPeer.BIRTHYEAR, him.getBirthyear());
		else
		criteria.add(BaseStudentPeer.BIRTHYEAR, year);
			
		if(bplace.length() == 0)	
		criteria.add(BaseStudentPeer.BIRTHPLACE, him.getBirthplace());
		else
		criteria.add(BaseStudentPeer.BIRTHPLACE, bplace);
		
		if(address.length() == 0)	
		criteria.add(BaseStudentPeer.ADDRESS, him.getAddress());
		else
		criteria.add(BaseStudentPeer.ADDRESS, address);

		if(parent.length() == 0)	
		criteria.add(BaseStudentPeer.PARENT, him.getParent());
		else
		criteria.add(BaseStudentPeer.PARENT, parent);
			
		if(occ.length() == 0)	
		criteria.add(BaseStudentPeer.OCCUPATION, him.getOccupation());
		else
		criteria.add(BaseStudentPeer.OCCUPATION, occ);
		
		if(medhist.length() == 0)	
		criteria.add(BaseStudentPeer.MEDICALHIST, him.getMedicalhist());
		else
		criteria.add(BaseStudentPeer.MEDICALHIST, medhist);
			
		if(medhist.length() == 0)	
			criteria.add(BaseStudentPeer.AGE, him.getAge());
		else
			criteria.add(BaseStudentPeer.AGE, age);
			
		
		try{
			BaseStudentPeer.doUpdate(criteria);
			return true;
		}catch(Exception e){
			e.printStackTrace();
			return false;
		}
	}
	
	@SuppressWarnings("unchecked")
	public static List<Student> retrieveAllStudents(){
		Criteria criteria = new Criteria();

		ViewAllStudentsController vasc = new ViewAllStudentsController();
		criteria = vasc.sortAllStudents(criteria);
		
		
		try{
			
			criteria.addSelectColumn(BaseStudentPeer.SURNAME);
			criteria.addSelectColumn(BaseStudentPeer.FIRSTNAME);
			criteria.addSelectColumn(BaseStudentPeer.MI);
			criteria.size();

			return BaseStudentPeer.doSelectVillageRecords(criteria);
		}catch(Exception e){
			e.printStackTrace();
			return new ArrayList<Student>();
		}
	}
	
}
